package zuoye06;

import java.util.function.IntConsumer;


public class FizzBuzz {
	private int n;
	private int num =1;
	private Object lock = new Object();

	public FizzBuzz(int n) {
		this.n = n;
	}

	public void fizz(Runnable printFizz) throws InterruptedException {
		synchronized (lock) {
			while(num<=n){
				if(num%3==0 && num%5!=0){
					printFizz.run();
					num++;
					lock.notifyAll();
				}else{
					lock.wait();
				}
			}
		}
	}
		public void buzz(Runnable printBuzz) throws InterruptedException {
			synchronized (lock) {
				while(num<=n){
					if(num%3!=0 && num%5==0){
						printBuzz.run();
						num++;
						lock.notifyAll();
					}else{
						lock.wait();
					}
				}
			}
		}

		public void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException {
			synchronized (lock) {
				while(num<=n){
					if(num%3==0 && num%5==0){
						printFizzBuzz.run();
						num++;
						lock.notifyAll();
					}else{
						lock.wait();
					}
				}
			}
		}

		public void number(IntConsumer printNumber) throws InterruptedException {
			synchronized (lock) {
				while(num<=n){
					if(num%3!=0 && num%5!=0){
						printNumber.accept(num);
						num++;
						lock.notifyAll();
					}else{
						lock.wait();
					}
				}

			}
		}
	}




